package DatabasePack;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Backup {

    public void BackupDatabase() {
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException cnfe) {
            System.err.println("Derby driver not found.");
        }

        try {
            System.out.println("GOING");
            Connection connBackup = DriverManager.getConnection("jdbc:derby://localhost:1527/Backup", "abdo", "123");
            Connection connBank = DriverManager.getConnection("jdbc:derby://localhost:1527/bank", "abdo", "123");
            
            Statement stmtBankATM = connBank.createStatement();
            Statement stmtBackupATM = connBackup.createStatement();
            Statement stmtBankBillPay = connBank.createStatement();
            Statement stmtBackupBillPay = connBackup.createStatement();
            Statement stmtBankClient = connBank.createStatement();
            Statement stmtBackupClient = connBackup.createStatement();
            Statement stmtBankCompany = connBank.createStatement();
            Statement stmtBackupCompany = connBackup.createStatement();
            Statement stmtBankFix = connBank.createStatement();
            Statement stmtBackupFix = connBackup.createStatement();
            Statement stmtBankTRANSACTI = connBank.createStatement();
            Statement stmtBackupTRANSACTI = connBackup.createStatement();

            String insertATM = "INSERT into ATM VALUES" + "(?,?,?,?)";
            String updateATM = "update ATM set MONEY=?";
            String insertBillPay = "INSERT into BILLBAY_TRANSACTION VALUES" + "(?,?,?,?,?)";
            String updateClient = "update CLIENT set MONEY=?, PASSWORD=?";
            String updateCompany = "update COMPANY set BALABNCE=?";
            String insertFix = "INSERT into FIX VALUES" + "(?,?,?,?)";
            String insertTRANSACTI = "INSERT into TRANSACTI VALUES" + "(?,?,?,?,?,?,?)";

            PreparedStatement pstmtATM = connBackup.prepareStatement(insertATM);
            PreparedStatement pstmtBillPay = connBackup.prepareStatement(insertBillPay);
            PreparedStatement pstmtFix = connBackup.prepareStatement(insertFix);
            PreparedStatement pstmtTRANSACTI = connBackup.prepareStatement(insertTRANSACTI);

            stmtBankATM.execute("SELECT * FROM ATM");
            stmtBackupATM.execute("SELECT * FROM ATM");
            stmtBankBillPay.execute("SELECT * FROM BILLBAY_TRANSACTION");
            stmtBackupBillPay.execute("SELECT * FROM BILLBAY_TRANSACTION");
            stmtBankClient.execute("SELECT * FROM CLIENT");
            stmtBackupClient.execute("SELECT * FROM CLIENT");
            stmtBankCompany.execute("SELECT * FROM COMPANY");
            stmtBackupCompany.execute("SELECT * FROM COMPANY");
            stmtBankFix.execute("SELECT * FROM FIX");
            stmtBackupFix.execute("SELECT * FROM FIX");
            stmtBankTRANSACTI.execute("SELECT * FROM TRANSACTI");
            stmtBackupTRANSACTI.execute("SELECT * FROM TRANSACTI");

            ResultSet rsBankATM = stmtBankATM.getResultSet();
            ResultSet rsBackupATM = stmtBackupATM.getResultSet();
            ResultSet rsBankBillPay = stmtBankBillPay.getResultSet();
            ResultSet rsBackupBillPay = stmtBackupBillPay.getResultSet();
            ResultSet rsBankClient = stmtBankClient.getResultSet();
            ResultSet rsBackupClient = stmtBackupClient.getResultSet();
            ResultSet rsBankCompany = stmtBankCompany.getResultSet();
            ResultSet rsBackupCompany = stmtBackupCompany.getResultSet();
            ResultSet rsBankFix = stmtBankFix.getResultSet();
            ResultSet rsBackupFix = stmtBackupFix.getResultSet();
            ResultSet rsBankTRANSACTI = stmtBankTRANSACTI.getResultSet();
            ResultSet rsBackupTRANSACTI = stmtBackupTRANSACTI.getResultSet();
                
            //ATM Backup
            while (rsBankATM.next()) {
                if (rsBackupATM.next()) {
                    PreparedStatement pstmt = connBackup.prepareStatement(updateATM + " where ATM_ID=" + rsBankATM.getInt("ATM_ID"));
                    //if id found update but there's still records in backup
                    if (rsBankATM.getString("ATM_ID").equalsIgnoreCase(rsBackupATM.getString("ATM_ID"))) {
                        pstmt.setInt(1, rsBankATM.getInt("MONEY"));
                        pstmt.execute();
                    }
                } // if there's no records left in backup , insert the new records from bank
                else {
                    pstmtATM.setInt(1, rsBankATM.getInt("ATM_ID"));
                    pstmtATM.setString(2, rsBankATM.getString("CITY"));
                    pstmtATM.setString(3, rsBankATM.getString("REGION"));
                    pstmtATM.setInt(4, rsBankATM.getInt("MONEY"));

                    pstmtATM.execute();
                }
            }
            
            //Billpay Backup
            while (rsBankBillPay.next()) {
                pstmtBillPay.setInt(1, rsBankBillPay.getInt("COMPANY_ID"));
                pstmtBillPay.setInt(2, rsBankBillPay.getInt("CLIENT_ID"));
                pstmtBillPay.setInt(3, rsBankBillPay.getInt("PHONE"));
                pstmtBillPay.setInt(4, rsBankBillPay.getInt("MONEY"));
                pstmtBillPay.setInt(5, rsBankBillPay.getInt("BILL_NUMBER"));

                pstmtBillPay.execute();
            }
            //Client backup
            while (rsBankClient.next()) {
                if (rsBackupClient.next()) {
                    PreparedStatement pstmt = connBackup.prepareStatement(updateClient + " where CLIENT_ID=" + rsBankClient.getInt("CLIENT_ID"));
                    //if id found update but there's still records in backup
                    if (rsBankClient.getInt("CLIENT_ID") == rsBackupClient.getInt("CLIENT_ID")) {
                        pstmt.setInt(1, rsBankClient.getInt("MONEY"));
                        pstmt.setInt(2, rsBankClient.getInt("PASSWORD"));
                        pstmt.execute();

                    }
                }
            }
            
            //company backup
            while (rsBankCompany.next()) {
                System.out.println("ENTERING");
                if (rsBackupCompany.next()) {
                    System.out.println("ENERING AGAIN");
                    PreparedStatement pstmt = connBackup.prepareStatement(updateCompany + " where ID=" + rsBankCompany.getInt("ID"));
                    //if id found update but there's still records in backup
                    if (rsBankCompany.getInt("ID") == rsBackupCompany.getInt("ID")) {
                        pstmt.setInt(1, rsBankCompany.getInt("BALABNCE"));
                        pstmt.execute();
                        System.out.println("OKIES");

                    }
                }
                else
                {
                    System.out.println("NO");
                }
            }
            //fix backup
            while (rsBankFix.next()) {
                pstmtFix.setInt(1, rsBankFix.getInt("EMPLOYEE_ID"));
                pstmtFix.setInt(2, rsBankFix.getInt("ATM_ID"));
                pstmtFix.setDate(3, rsBankFix.getDate("DATE"));
                pstmtFix.setTime(4, rsBankFix.getTime("TIME"));

                pstmtFix.execute();
            }

            //Interaction backup
            while (rsBankTRANSACTI.next()) {
                pstmtTRANSACTI.setInt(1, rsBankTRANSACTI.getInt("CLIENT_ID"));
                pstmtTRANSACTI.setInt(2, rsBankTRANSACTI.getInt("RECIEVER_ID"));
                pstmtTRANSACTI.setInt(5, rsBankTRANSACTI.getInt("MONEY"));
                pstmtTRANSACTI.setDate(3, rsBankTRANSACTI.getDate("DATE"));
                pstmtTRANSACTI.setTime(4, rsBankTRANSACTI.getTime("TIME"));
                pstmtTRANSACTI.setInt(6, rsBankTRANSACTI.getInt("KIND_OF_TRANSACTION"));
                pstmtTRANSACTI.setInt(7, rsBankTRANSACTI.getInt("ATM_ID"));

                pstmtTRANSACTI.execute();
            }
            connBackup.close();
            connBank.close();
        } catch (SQLException ex) {
        }
    }
}